#!/usr/bin/python
 
import smtplib
import os
import email
from email import *
from email.MIMEMultipart import *
from email.Utils import COMMASPACE,formatdate
from email import Encoders
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
import sys
 
#recuperation des parametres.
 
send_from,send_to,subject,text,files = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5:]
    
def sendmail(send_from, send_to, subject, text, files=[], server="smtp.free.fr"):
 
  msg = MIMEMultipart()
  msg['From'] = send_from
  msg['To'] = send_to
  msg['Date'] = formatdate(localtime=True)
  msg['Subject'] = subject
 
  msg.attach( MIMEText(text) )
 
  for fichiers in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(fichiers,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(fichiers))
    msg.attach(part)
 
  smtp = smtplib.SMTP(server)
  #a mon avis la ligne suivante est necessaire pour utiliser gmail
  #si ca marche pas essayere sans
  #smtp.starttls()
  #smtp.login("user", "password")
  smtp.sendmail(send_from, send_to, msg.as_string())
  smtp.close()
 
 
 
if __name__=="__main__":
 
  if len(sys.argv) < 6:
     print("Missing arguments... exiting")
     exit
 
  print("FROM:"+send_from)
  print("TO:"+send_to)
  print("SUBJECT:"+subject)
  print("TEXT:"+text)
  print("FILES:"+str(files))
 
  sendmail(send_from,send_to,subject,text,files)
